package org.chen.controller;

import lombok.extern.slf4j.Slf4j;
import org.chen.common.Feedback;
import org.chen.common.FeedbackResponse;
import org.chen.common.ReportMessage;
import org.chen.common.UpdateInfo;
import org.chen.result.ResultBody;
import org.chen.service.FeedbackService;
import org.chen.service.ReportMessageService;
import org.chen.service.UpdateEnInfoService;
import org.chen.utils.ControllerMethodTitle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/ai")
public class FeedBackController {

    @Autowired
    private FeedbackService feedbackService;

    @Autowired
    private ReportMessageService reportMessageService;

    @Autowired
    private UpdateEnInfoService updateInfoService;

    @PostMapping("/feedback")
    private FeedbackResponse insert(@RequestBody Feedback feedback){
        System.out.println(feedback);
        try {
            boolean insert = feedbackService.save(feedback);
            if (insert) {
        return FeedbackResponse.getFeedbackResponse(true, "The feedback was submitted successfully");
            } else {
                return FeedbackResponse.getFeedbackResponse(false, "Feedback submission failed, please try again");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return FeedbackResponse.getFeedbackResponse(false, "Feedback submission error, please try again");
        }
    }

    @PostMapping("/report")
    private FeedbackResponse report(@RequestBody ReportMessage reportMessage){
        System.out.println(reportMessage);
        try {
            boolean insert = reportMessageService.save(reportMessage);
            if (insert) {
                return FeedbackResponse.getFeedbackResponse(true, "The reportMessage was submitted successfully");
            } else {
                return FeedbackResponse.getFeedbackResponse(false, "ReportMessage submission failed, please try again");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return FeedbackResponse.getFeedbackResponse(false, "ReportMessage submission error, please try again");
        }
    }

    @GetMapping("/en_updates")
    @ControllerMethodTitle("en")
    public ResultBody<UpdateInfo> getEnUpdateInfo() {
        return ResultBody.success(updateInfoService.getLatestEnUpdateInfo());
    }

    @GetMapping("/zh_updates")
    @ControllerMethodTitle("zh")
    public ResultBody<UpdateInfo> getZhUpdateInfo() {
        return ResultBody.success(updateInfoService.getLatestZhUpdateInfo());
    }
}
